#!/bin/bash

set -o nounset
set -o errexit
set -o noclobber


HOSTS_FILE="/etc/hosts"
NETWORK_FILE="/etc/sysconfig/network"
SELINUX_FILE="/etc/selinux/config"

ETH_FILE="/etc/sysconfig/network-scripts/ifcfg-eth0"
HOSTNAME_FILE="/etc/hostname"
DNS_CONFIG_FILE="/etc/sysconfig/network-scripts/ifcfg-eth0"

source function_cat_wrap.include


#####################################################################

function extra_packages() {

yum -y install perl

# Necessary for ifconfig in centos 7

yum -y install net-tools.x86_64
yum -y install deltarpm
}

#####################################################################


function change_hostname() {
echo " "

echo " Current hostname is ${HOSTNAME_FQDN}"
echo " External IP is      ${HOSTNAME_IP}"
echo " "
echo " "
echo "Please enter the FQDN you'd like to have"
printf " > "
read NEW_FQDN

NEW_HOST=`echo "${NEW_FQDN}" | cut -d "." -f 1 `

echo "${HOSTNAME_IP}    ${NEW_FQDN}     ${NEW_HOST}" >> ${HOSTS_FILE}

# The CentOS 6 way...
sed -i "/^HOSTNAME/d" ${NETWORK_FILE}
sed -i "/^NETWORKING/d" ${NETWORK_FILE}

echo "NETWORKING=yes" >> ${NETWORK_FILE}
echo "HOSTNAME=${NEW_FQDN}" >> ${NETWORK_FILE}

: '

# The CentOS 7 way...
rm -f  $HOSTNAME_FILE
echo "$NEW_FQDN" > $HOSTNAME_FILE

'

}

#####################################################################
function add_dns_resolution() {

sed -i "/DNS1=/d" ${DNS_CONFIG_FILE}
sed -i "/DNS2=/d" ${DNS_CONFIG_FILE}

echo "DNS1=8.8.8.8" >> ${DNS_CONFIG_FILE}
echo "DNS2=8.8.4.4" >> ${DNS_CONFIG_FILE}



}



#####################################################################
function change_password() {
echo " "
echo " "
echo "Please change your root password..."
passwd
}

#####################################################################
function disable_selinux() {
sed -i "s/^SELINUX=.*$/SELINUX=disabled/g" ${SELINUX_FILE}
}

#####################################################################
function yum_update_upgrade() {
echo "Now going to yum update and upgrade, then I'll reboot the system for you."
#echo "Go get some coffee or something.  This will take awhile."

yum -y  update
yum -y  upgrade
}

#----------------------------------------------------------------
#----------------------------------------------------------------
#----------------------------------------------------------------
#----------------------------------------------------------------
#----------------------------------------------------------------

echo "This is a preconfiguration program"


echo ""
echo " 1. Change hostname"
echo " 2. Change password"
echo " 3. Disable SELINUX"
echo " 4. yum update & upgrade"
echo " 5. Reboot"

echo " "

echo "Want to start? y for yes, anything else is no"
read RESP

if [ "${RESP}" != "y" ]
then
	exit
else


HOSTNAME_FQDN=`hostname -f`
#HOSTNAME_IP=`hostname -i`
#HOSTNAME_IP=`ifconfig eth0 | grep "inet addr" | tr ":" " " | tr -s " " | cut -d " " -f 4 `
HOSTNAME_IP=`grep IPADDR $ETH_FILE | cut -d "=" -f 2 `


change_hostname
disable_selinux

###################


cat_wrap ${HOSTS_FILE}

cat_wrap ${NETWORK_FILE}

cat_wrap ${SELINUX_FILE}

# The next line is only for CentOS 7
#cat_wrap ${HOSTNAME_FILE}

cat_wrap ${DNS_CONFIG_FILE}




echo "Look okay?"
printf " > "
read a

#####################

change_password

yum_update_upgrade

echo " "
echo " "

reboot

fi
